package com.genesis.yunnanji.view;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.genesis.yunnanji.Constants;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.BrandDetailsAdapter;
import com.genesis.yunnanji.bean.BaseBean;
import com.genesis.yunnanji.bean.BrandDetailsBean;
import com.genesis.yunnanji.bean.BrandDetailsBeanList;
import com.genesis.yunnanji.utils.NetUtils;
import com.genesis.yunnanji.widget.divider.SpacesItemDecoration;
import com.youth.xframe.widget.XToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;

/**
 * 创建日期：2018/9/30 003014:28
 * 作者：Kite
 * 描述：品牌二级界面
 */
public class BrandDetailsActivity extends BaseActivity {

    @BindView(R.id.iv_bg)
    ImageView ivBg;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.toolbar_layout)
    CollapsingToolbarLayout toolbarLayout;

    @BindView(R.id.rv_brand_details)
    RecyclerView rvBrandDetails;

    @BindView(R.id.tv_one)
    TextView tvOne;

    @BindView(R.id.view_one)
    View viewOne;

    @BindView(R.id.tv_two)
    TextView tvTwo;

    @BindView(R.id.view_two)
    View viewTwo;

    @BindView(R.id.tv_three)
    TextView tvThree;

    @BindView(R.id.view_three)
    View viewThree;

    @BindView(R.id.tv_four)
    TextView tvFour;

    @BindView(R.id.view_four)
    View viewFour;

    @BindView(R.id.tv_describe)
    TextView tvDescribe;

    private boolean twoClick = true;

    private boolean threeClick = true;

    private BrandDetailsAdapter mBrandDetailsAdapter;

    private List<BrandDetailsBeanList.ListBean> mBrandDetailsBeanList = new ArrayList<>();

    private String mGoodsID = "";//品牌商品ID

    private String mCategoryId = "";//品类商品ID

    private String mSale = "";//销量排序,asc 顺序，desc倒序

    private String mPrice = "";//价格排序asc 顺序，desc倒序

    private String mTime = "";//时间排序asc 顺序，desc倒序

    private SpacesItemDecoration spacesItemDecoration;//RecyclerView间距

    @Override
    public int getLayoutId() {
        return R.layout.activity_brand_details;
    }

    @Override
    public void initView() {
        super.initView();
        Bundle bundle = null;
        if (bundle == null) {
            setTitle(getIntent().getStringExtra("p0"));
            if (getIntent().getStringExtra("p2") != null) {
                toolbarLayout.setVisibility(View.VISIBLE);
                mGoodsID = getIntent().getStringExtra("p1");
                getGoodsDetails();
                getGoodsList(mGoodsID, "");
            } else {
                toolbarLayout.setVisibility(View.GONE);
                mCategoryId = getIntent().getStringExtra("p1");
                getGoodsList("", mCategoryId);
            }
        }
        addMenuImageItem(R.drawable.top_cart, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        spacesItemDecoration = new SpacesItemDecoration(22, 22, getResources().getColor(R.color.color_fff));
    }

    @OnClick({R.id.tv_story, R.id.ll_one, R.id.ll_two, R.id.ll_three, R.id.ll_four})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_story:
                break;
            case R.id.ll_one:
                clickLayout(0);
                break;
            case R.id.ll_two:
                clickLayout(1);
                break;
            case R.id.ll_three:
                clickLayout(2);
                break;
            case R.id.ll_four:
                clickLayout(3);
                break;
        }
    }

    private void clickLayout(int place) {
        switch (place) {
            case 0:
                twoClick = true;
                threeClick = true;
                isVisible(tvOne, viewOne, true);
                isVisible(tvTwo, viewTwo, false);
                isVisible(tvThree, viewThree, false);
                isVisible(tvFour, viewFour, false);
                defaultState(tvTwo, R.drawable.order);
                defaultState(tvThree, R.drawable.order);
                mSale = "";
                mPrice = "";
                mTime = "";
                getGoodsList(mGoodsID, mCategoryId);
                break;
            case 1:
                threeClick = true;
                if (twoClick) {
                    defaultState(tvTwo, R.drawable.order_down);
                    mTime = "asc";
                    mSale = "";
                    mPrice = "";
                    twoClick = false;
                    getGoodsList(mGoodsID, mCategoryId);
                } else {
                    defaultState(tvTwo, R.drawable.order_up);
                    mTime = "desc";
                    mSale = "";
                    mPrice = "";
                    twoClick = true;
                    getGoodsList(mGoodsID, mCategoryId);
                }
                isVisible(tvOne, viewOne, false);
                isVisible(tvTwo, viewTwo, true);
                isVisible(tvThree, viewThree, false);
                isVisible(tvFour, viewFour, false);
                defaultState(tvThree, R.drawable.order);
                break;
            case 2:
                twoClick = true;
                if (threeClick) {
                    defaultState(tvThree, R.drawable.order_down);
                    mPrice = "desc";
                    mSale = "";
                    mTime = "";
                    threeClick = false;
                    getGoodsList(mGoodsID, mCategoryId);
                } else {
                    defaultState(tvThree, R.drawable.order_up);
                    mPrice = "asc";
                    mSale = "";
                    mTime = "";
                    threeClick = true;
                    getGoodsList(mGoodsID, mCategoryId);
                }
                isVisible(tvOne, viewOne, false);
                isVisible(tvTwo, viewTwo, false);
                isVisible(tvThree, viewThree, true);
                isVisible(tvFour, viewFour, false);
                defaultState(tvTwo, R.drawable.order);
                break;
            case 3:
                twoClick = true;
                threeClick = true;
                mSale = "asc";
                mPrice = "";
                mTime = "";
                getGoodsList(mGoodsID, mCategoryId);
                isVisible(tvOne, viewOne, false);
                isVisible(tvTwo, viewTwo, false);
                isVisible(tvThree, viewThree, false);
                isVisible(tvFour, viewFour, true);
                defaultState(tvTwo, R.drawable.order);
                defaultState(tvThree, R.drawable.order);
                break;
            default:
                twoClick = true;
                threeClick = true;
                isVisible(tvOne, viewOne, true);
                isVisible(tvTwo, viewTwo, false);
                isVisible(tvThree, viewThree, false);
                isVisible(tvFour, viewFour, false);
                defaultState(tvTwo, R.drawable.order);
                defaultState(tvThree, R.drawable.order);
                mSale = "";
                mPrice = "";
                mTime = "";
                getGoodsList(mGoodsID, mCategoryId);
                break;
        }
    }

    private void isVisible(TextView textView, View view, boolean click) {
        if (click) {
            textView.setTextColor(getResources().getColor(R.color.color_333));
            view.setVisibility(View.VISIBLE);
        } else {
            textView.setTextColor(getResources().getColor(R.color.color_999));
            view.setVisibility(View.GONE);
        }
    }

    private void defaultState(TextView textView, int drawableRes) {
        Drawable drawable = getResources().getDrawable(drawableRes);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable, null);
    }

    private void getDate() {
        rvBrandDetails.setLayoutManager(new GridLayoutManager(this, 2));
        mBrandDetailsAdapter = new BrandDetailsAdapter(this, rvBrandDetails, mBrandDetailsBeanList);
        rvBrandDetails.setAdapter(mBrandDetailsAdapter);
        mBrandDetailsAdapter.notifyDataSetChanged();
        rvBrandDetails.setItemAnimator(new DefaultItemAnimator());
        rvBrandDetails.removeItemDecoration(spacesItemDecoration);
        rvBrandDetails.addItemDecoration(spacesItemDecoration);
//        rvBrandDetails.addItemDecoration(new SpacesItemDecoration(22, 22, getResources().getColor(R.color.color_fff)));
//        rvBrandDetails.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 22, getResources().getColor(R.color.color_fff)));
//        rvBrandDetails.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 22, getResources().getColor(R.color.color_fff)));
    }

    private void getGoodsDetails() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", mGoodsID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = NetUtils.initNet(params);
        EasyHttp.post(Constants.INDEX_BRANDVIEW)
//                .baseUrl(Constants.MAIN_ADMIN)
                .requestBody(requestBody)
                .execute(new CallBackProxy<BaseBean<BrandDetailsBean>, BrandDetailsBean>(new SimpleCallBack<BrandDetailsBean>() {
                    @Override
                    public void onError(ApiException e) {
                        XToast.error(e.getMessage());
                    }

                    @Override
                    public void onSuccess(BrandDetailsBean bean) {
                        tvTitle.setText(bean.getInfo().getShop_name());
                        tvDescribe.setText(bean.getInfo().getDescription());
                        Glide.with(BrandDetailsActivity.this).load(Constants.PIC_ADMIN + bean.getInfo().getShop_img()).into(ivBg);
                    }
                }) {
                });
    }

    private void getGoodsList(String shopid, String categoryid) {
        JSONObject params = new JSONObject();
        try {
            params.put("p", 1);
            params.put("row", 10);
            params.put("shopid", shopid);
            params.put("categoryid", categoryid);
            params.put("sale", mSale);
            params.put("price", mPrice);
            params.put("time", mTime);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = NetUtils.initNet(params);
        EasyHttp.post(Constants.INDEX_GETPRODUCT)
//                .baseUrl(Constants.MAIN_ADMIN)
                .requestBody(requestBody)
                .execute(new CallBackProxy<BaseBean<BrandDetailsBeanList>, BrandDetailsBeanList>(new SimpleCallBack<BrandDetailsBeanList>() {
                    @Override
                    public void onError(ApiException e) {
                        XToast.error(e.getMessage());
                    }

                    @Override
                    public void onSuccess(BrandDetailsBeanList bean) {
                        mBrandDetailsBeanList.clear();
                        mBrandDetailsBeanList = bean.getList();
                        getDate();
                    }
                }) {
                });
    }

}
